﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Chefbook.DataObjects;
using System.Globalization;
using Chefbook.ViewModels;

namespace Chefbook.UserActions
{
    /// <summary>
    /// An action to save a recipe
    /// </summary>
    public class SaveRecipeAction : UserActionBase
    {
        private RecipeViewModel oldRecipeData, recipeToSaveTo, tempRecipeData;

        /// <summary>
        /// Creates A new action to save recipe data into an actual recipe
        /// </summary>
        /// <param name="recipeToSaveTo"></param>
        /// <param name="tempRecipeData"></param>
        public SaveRecipeAction(RecipeViewModel recipeToSaveTo, RecipeViewModel tempRecipeData)
            : base()
        {
            this.recipeToSaveTo = recipeToSaveTo;
            this.oldRecipeData = recipeToSaveTo.MemberwiseClone();
            this.tempRecipeData = tempRecipeData;
        }

        /// <summary>
        /// Saves the recipe
        /// </summary>
        public override void DoAction()
        {
            base.DoAction();
            CopyRecipe(recipeToSaveTo, tempRecipeData);
        }

        /// <summary>
        /// Reverts the recipe to the previous save
        /// </summary>
        public override void UndoAction()
        {
            base.UndoAction();
            CopyRecipe(recipeToSaveTo, oldRecipeData);
        }

        /// <summary>
        /// Returns a user-friendly string describing the action
        /// </summary>
        /// <returns>A user-friendly string describing the action</returns>
        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentUICulture, "Save Recipe '{0}'", tempRecipeData);
        }

        private static void CopyRecipe(RecipeViewModel CopyTo, RecipeViewModel CopyFrom)
        {
            CopyTo.Name = CopyFrom.Name;
            CopyTo.Description = CopyFrom.Description;
            CopyTo.Directions = CopyFrom.Directions;
            CopyTo.RecipeModel.PrepTime = CopyFrom.RecipeModel.PrepTime;
            CopyTo.RecipeModel.CookTime = CopyFrom.RecipeModel.CookTime;
            List<CategoryViewModel> cats = new List<CategoryViewModel>(CopyTo.Categories);
            foreach (var category in cats)
            {
                category.RemoveRecipe(CopyTo);
            }
            cats = new List<CategoryViewModel>(CopyFrom.Categories);
            foreach (var category in cats)
            {
                category.AddRecipe(CopyTo);
            }
            List<IngredientViewModel> ings = new List<IngredientViewModel>(CopyTo.Ingredients);
            foreach (var ingredient in ings)
            {
                CopyTo.RemoveIngredient(ingredient);
            }
            ings = new List<IngredientViewModel>(CopyFrom.Ingredients);
            foreach (var ingredient in ings)
            {
                var newIng = new IngredientViewModel(new Ingredient());
                newIng.Name = ingredient.Name;
                newIng.Amount = ingredient.Amount;
                newIng.Units = ingredient.Units;
                CopyTo.AddIngredient(newIng);
            }
        }
    }
}
